package com.bnyy.medicalHousekeeper.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.SpanUtils;
import com.bnyy.common.adapter.BaseNormalListAdapter;
import com.bnyy.medicalHousekeeper.GlideHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.activity.NormalGoodsDetailActivity;
import com.bnyy.medicalHousekeeper.activity.ServiceGoodsDetailActivity;
import com.bnyy.medicalHousekeeper.bean.Goods;
import com.bnyy.medicalHousekeeper.bean.Tag;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.text.DecimalFormat;
import java.util.HashMap;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2023/2/6
 **/
public class MedicalHousekeeperGoodsAdapter extends BaseNormalListAdapter<Goods, MedicalHousekeeperGoodsAdapter.ViewHolder> {

    private int dp12;

    public MedicalHousekeeperGoodsAdapter(Context context) {
        super(context);
        dp12 = SizeUtils.dp2px(12);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.item_medical_housekeeper_goods, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);

        Goods goods = getData(position);

        GlideHelper.setRoundImage(mContext, String.valueOf(goods.getGoods_image()), holder.iv, dp12);

        holder.tvGoodsName.setText(goods.getGoods_name());
        SpanUtils.with(holder.tvPrice)
                .append("¥").setFontSize(10, true)
                .append(new DecimalFormat("0.00").format(goods.getPrice()))
                .create();

        holder.tvShopName.setText(goods.getShop_name());

        if (goods.getType() == 2) {
            GradientDrawable bg = new GradientDrawable();
            bg.setCornerRadii(new float[]{
                    dp12, dp12,
                    dp12, dp12,
                    0, 0,
                    0, 0
            });
            int type = goods.getType();
            bg.setColor(Color.parseColor(type == 1 ? "#8030C159" : "#80FFA13D"));
            holder.tvType.setBackground(bg);
            holder.tvType.setText(goods.getType_desc());
        }
        if (goods.getTags() != null) {
            holder.tagFlowLayout.setAdapter(new TagAdapter<Tag>(goods.getTags()) {
                int margin = SizeUtils.dp2px(5);

                @Override
                public View getView(FlowLayout parent, int position, Tag tag) {
                    ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT);
                    lp.rightMargin = margin;

                    TextView tv = new TextView(mContext);
                    tv.setLayoutParams(lp);
                    tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
                    tv.setPadding(10, 5, 10, 5);
                    tv.setTextColor(Color.parseColor(tag.getTextColor()));
                    GradientDrawable bg = new GradientDrawable();
                    bg.setCornerRadius(5);
                    bg.setColor(Color.parseColor(tag.getBackgroundColor()));
                    tv.setBackground(bg);
                    tv.setText(tag.getText());

                    return tv;
                }
            });
        }

        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (goods.getType()) {
                    case 1:
//                        HashMap<String, Object> params = new HashMap<>();
//                        params.put("goods_id", goods.getGoods_id());
//                        params.put("shop_id", goods.getShop_id());
//                        RequestManager requestManager = RequestManager.getInstance();
//                        requestManager.request(requestManager.mMallRetrofitService.getGoodsDetail(RequestManager.getJsonRequestBody(params)),
//                                new BaseObserverImpl<GoodsDetail>(mContext) {
//                                    @Override
//                                    public void onSuccess(GoodsDetail goodsDetail) {
//                                        super.onSuccess(goodsDetail);
//
//                                    }
//                                });

                        NormalGoodsDetailActivity.show(mContext, new Goods(goods.getGoods_id(), goods.getShop_id()));
                        break;
                    case 2:
                    case 3:
                        ServiceGoodsDetailActivity.show(mContext, goods.getId());
                        break;
                }
            }
        });
    }

    static class ViewHolder extends BaseNormalListAdapter.ViewHolder {

        ImageView iv;
        TextView tvGoodsName;
        TextView tvType;
        TextView tvPrice;
        TextView tvShopName;
        TagFlowLayout tagFlowLayout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            iv = itemView.findViewById(com.bnyy.medicalHousekeeper.R.id.iv);
            tvGoodsName = itemView.findViewById(com.bnyy.medicalHousekeeper.R.id.tv_goods_name);
            tvType = itemView.findViewById(com.bnyy.medicalHousekeeper.R.id.tv_type);
            tvPrice = itemView.findViewById(com.bnyy.medicalHousekeeper.R.id.tv_price);
            tvShopName = itemView.findViewById(com.bnyy.medicalHousekeeper.R.id.tv_shop_name);
            tagFlowLayout = itemView.findViewById(com.bnyy.medicalHousekeeper.R.id.tag_flow_layout);
        }
    }
}





















